package com.tao.demo.controller.sys;


import com.tao.demo.annotation.MultiRequestBody;
import com.tao.demo.core.domain.vo.R;
import com.tao.demo.domain.dto.FileChunkDTO;
import com.tao.demo.domain.dto.FileChunkResultDTO;
import com.tao.demo.service.UploadService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;


/**
 * @ProjectName UploaderController
 * @author Administrator
 * @version 1.0.0
 * @Description 附件分片上传
 * @createTime 2022/4/13 0013 15:58
 */
@Log4j2
@RestController
@RequestMapping("upload")
public class UploaderController {
 
   @Resource
   private UploadService uploadService;
 
   /**
    * 检查分片是否存在
    *
    * @return
    */
   @GetMapping()
   public R<FileChunkResultDTO> checkChunkExist(FileChunkDTO chunkDTO) {
      FileChunkResultDTO fileChunkCheckDTO;
      try {
         fileChunkCheckDTO = uploadService.checkChunkExist(chunkDTO);
         return R.success(fileChunkCheckDTO);
      } catch (Exception e) {
         return R.error(e.getMessage());
      }
   }
 
 
   /**
    * 上传文件分片
    *
    * @param chunkDTO
    * @return
    */
   @PostMapping()
   public R<String> uploadChunk(@MultiRequestBody FileChunkDTO chunkDTO){
      try {
         uploadService.uploadChunk(chunkDTO);
         return R.success(chunkDTO.getIdentifier());
      } catch (Exception e) {
         return R.error(e.getMessage());
      }
   }
 
   /**
    * 请求合并文件分片
    *
    * @param chunkDTO
    * @return
    */
   @PostMapping("merge")
   public R<Boolean> mergeChunks(@RequestBody FileChunkDTO chunkDTO) {
      try {
         boolean success = uploadService.mergeChunk(chunkDTO.getIdentifier(), chunkDTO.getFilename(), chunkDTO.getTotalChunks());
         return R.success(success);
      } catch (Exception e) {
         return R.error(e.getMessage());
      }
   }
 
}